package com.chinatechstar.decis.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.base.entity.SysWell;
import com.chinatechstar.component.commons.exception.AjaxExceptionEnum;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import com.chinatechstar.component.commons.utils.NumericUtils;
import com.chinatechstar.component.commons.utils.ValidateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinatechstar.decis.entity.DecisEconInput;
import com.chinatechstar.decis.mapper.DecisEconInputMapper;
import com.chinatechstar.decis.service.DecisEconInputService;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 业务逻辑实现层
 *
 * @author Rei
 */
@Service
@Transactional
public class DecisEconInputServiceImpl implements DecisEconInputService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final SequenceGenerator SEQUENCE_GENERATOR = new SequenceGenerator();

    @Autowired
    private DecisEconInputMapper decisEconInputMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询所有数据
     */
    @Override
    public List<DecisEconInput> findDecisEconInputAll() {
        return decisEconInputMapper.findDecisEconInputAll();
    }

    /**
     * 查询分页
     */
    @Override
    public Map<String, Object> findDecisEconInputByCondition(Integer currentPage, Integer pageSize, String name, String sorter) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        if (StringUtils.isNotBlank(sorter)) {
            String sort = sorter.substring(0, sorter.lastIndexOf('_'));
            String sequence = "ascend".equals(sorter.substring(sorter.lastIndexOf('_') + 1)) ? "ASC" : "DESC";
            paramMap.put("sort", sort);
            paramMap.put("sequence", sequence);
        } else {
            paramMap.put("sort", "createTime");
            paramMap.put("sequence", "DESC");
        }
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList = decisEconInputMapper.findDecisEconInputByCondition(paramMap);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), currentPage, pageSize);
    }

    /**
     * 查询导出数据列表
     */
    @Override
    public List<LinkedHashMap<String, Object>> findDecisEconInputForExcel(Map<String, Object> paramMap) {
        return decisEconInputMapper.findDecisEconInputByCondition(paramMap);
    }

    /**
     * 根据主键ID数据查询数据
     */
    @Override
    public DecisEconInput findDecisEconInputById(Long id) {
        return decisEconInputMapper.findDecisEconInputById(id);
    }

    /**
     * 根据主键ID数组查询数据
     */
    @Override
    public List<DecisEconInput> findDecisEconInputByIds(Long[] ids) {
        return decisEconInputMapper.findDecisEconInputByIds(ids);
    }



    /**
     * 批量新增
     */
    @Override
    public void insertDecisEconInputs(List<DecisEconInput> decisEconInputs) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        for (DecisEconInput decisEconInput : decisEconInputs) {
            decisEconInput.setId(SEQUENCE_GENERATOR.nextId());
            decisEconInput.setCreateName(currUser.getNickname());
            decisEconInput.setCreateUsername(currUser.getUsername());
        }
        decisEconInputMapper.insertDecisEconInputs(decisEconInputs);
    }

    /**
     * 编辑
     */
    @Override
    public void updateDecisEconInput(DecisEconInput decisEconInput) {
        decisEconInputMapper.updateDecisEconInput(decisEconInput);
        logger.info("已编辑： {}", decisEconInput.getId());
    }

    /**
     * 批量编辑
     */
    @Override
    public void updateDecisEconInputs(List<DecisEconInput> decisEconInputs) {
        decisEconInputMapper.updateDecisEconInputs(decisEconInputs);
    }

    /**
     * 删除
     */
    @Override
    public void deleteDecisEconInputById(Long id) {
        decisEconInputMapper.deleteDecisEconInputById(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteDecisEconInputByIds(Long[] ids) {
        decisEconInputMapper.deleteDecisEconInputByIds(ids);
    }

}
